<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Storefront_Model
 * @subpackage ProductFeature
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for product feature
 * 
 * @category   Zend
 * @package    Storefront_Model
 * @subpackage ProductFeature
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Storefront_Model_ProductFeature extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'product_feature';

    /**
     * Get product features for a product
     * @param integer $productId 
     * @param integer $languageId 
     * @return array 
     */
    function getAllByProductAndLanguage($productId, $languageId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('pf' => $this->_name), '');
        $query->join(array('pft' => 'product_feature_translation'), 'pft.product_feature_id = pf.id', 'pft.presentation_value');
        $query->join(array('cf' => 'category_feature'), 'pf.category_feature_id = cf.id', '');
        $query->join(array('f' => 'feature'), 'cf.feature_id = f.id', '');
        $query->join(array('ft' => 'feature_translation'), 'ft.feature_id = f.id', 'ft.name as feature_name');
        $query->join(array('cfg' => 'category_feature_group'), 'cf.category_feature_group_id = cfg.id', '');
        $query->join(array('fg' => 'feature_group'), 'cfg.feature_group_id = fg.id', '');
        $query->join(array('fgt' => 'feature_group_translation'), 'fgt.feature_group_id = fg.id', 'fgt.name as feature_group_name');
        $query->where('pf.product_id = ?', $productId);
        $query->where('pft.language_id = ?', $languageId);
        $query->where('ft.language_id = ?', $languageId);
        $query->where('fgt.language_id = ?', $languageId);
        $query->order(array('cfg.no DESC', 'cf.searchable DESC', 'cf.no DESC'));
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        // read data about product features from result
        foreach ($result as $row) {
            $row->presentation_value = str_replace("\\n", ', ', $row->presentation_value);
        }
        return $result;
    }

}
